using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Home_AllocateVenues : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    #region Helper Functions
    protected string GetCampusName(string sPlacementTypeId)
    {
        PlacementManager.PlacementTypeComponent oPlacementTypeC = new PlacementManager.PlacementTypeComponent();
        int campusId = oPlacementTypeC.SelectOne(Int32.Parse(sPlacementTypeId)).CampusId;

        PlacementManager.CampusComponent oCampusC = new PlacementManager.CampusComponent();
        return oCampusC.SelectOne(campusId).CampusCode;
    }
    protected string GetUnitName(string unitId)
    {
        PlacementManager.UnitComponent oUnitC = new PlacementManager.UnitComponent();
        return oUnitC.SelectOne(Int32.Parse(unitId)).UnitCode + " - " + oUnitC.SelectOne(Int32.Parse(unitId)).UnitName;
    }
    protected string GetVenueName(string venueId)
    {
        PlacementManager.VenueComponent oVenueC = new PlacementManager.VenueComponent();
        return oVenueC.SelectOne(Int32.Parse(venueId)).VenueName;
    }
    protected string GetCategoryName(string categoryId)
    {
        PlacementManager.CategoryComponent oCategoryC = new PlacementManager.CategoryComponent();
        return oCategoryC.SelectOne(Int32.Parse(categoryId)).CategoryName;
    }
    protected string GetVenueDetails(string sVenuePlacementTypeId)
    {
        //Get the varaiables for this venue for this placement type
        int venuePlacementTypeId = Int32.Parse(sVenuePlacementTypeId);
        PlacementManager.LinkVenuePlacementTypeComponent oLvptC = new PlacementManager.LinkVenuePlacementTypeComponent();
        PlacementManager.LinkVenuePlacementType oLvpt = oLvptC.SelectOne(venuePlacementTypeId);
        int maxStudents = oLvpt.MaxNumStudents;
        bool isRestricted = oLvpt.IsRestricted;
        int venueId = oLvpt.VenueId;

        //Get the venue name
        PlacementManager.VenueComponent oVenueC = new PlacementManager.VenueComponent();
        string venueName = oVenueC.SelectOne(venueId).VenueName;
        if (venueName.Length > 30)
        {
            venueName = venueName.Substring(0, 31) + "...";
        }

        //Get the number of allocations
        PlacementManager.AllocationComponent oAllocationC = new PlacementManager.AllocationComponent();
        List<PlacementManager.Allocation> oAllocationList = oAllocationC.SelectAllWVenuePlacementTypeId(venuePlacementTypeId, "AllocationId");
        int numAllocated = oAllocationList.Count;

        string sToReturn = "<span style=\"font-weight:bold;\"><a href='Venues.aspx?id=" + venueId.ToString() + "'>" + venueName + "</a></span>";
        sToReturn += "<br /><span class=\"gridViewSmallText\">Number Students Allocated:" + numAllocated.ToString();
        sToReturn += " <br /><span class=\"gridViewSmallText\">Number Places:" + maxStudents.ToString();
        if (isRestricted)
        {
            sToReturn += " Max Num:" + maxStudents.ToString() + "</span>";

            if (numAllocated >= maxStudents)
            {
                sToReturn += "<br /><span class=\"message\">This venue is full.</span>";
            }
        }
        return sToReturn;
    }
    protected string GetStudentDetails(string sUserId)
    {
        PlacementManager.UserComponent oUserC = new PlacementManager.UserComponent();
        PlacementManager.User oUser = oUserC.SelectOne(Int32.Parse(sUserId));

        return oUser.IdNumber + " - " + oUser.LastName + ", " + oUser.FirstName;
    }
    #endregion

    protected void gvPlacementTypes_DataBound(object sender, EventArgs e)
    {
        this.gvVenues.DataBind();
        this.gvAvailableVenues.DataBind();
    }
    protected void gvPlacementTypes_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvPlacementTypes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string toolTip = this.GetUnitName(DataBinder.Eval(e.Row.DataItem, "UnitId").ToString());
            toolTip += "<br /> Semester:" + DataBinder.Eval(e.Row.DataItem, "SemesterId").ToString();
            toolTip += "<br /> Year:" + DataBinder.Eval(e.Row.DataItem, "Year").ToString();
            toolTip += "<br /> Campus:" + this.GetCampusName(DataBinder.Eval(e.Row.DataItem, "PlacementTypeId").ToString());
            toolTip += "<br /> Category:" + this.GetCategoryName(DataBinder.Eval(e.Row.DataItem, "CategoryId").ToString());
            toolTip += "<br /> Start Date:" + (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DateStart"))).ToShortDateString();
            toolTip += "<br /> End Date:" + (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DateEnd"))).ToShortDateString();


            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");
            gvr.Cells[2].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[2].Attributes.Add("onmouseout", "hideToolTip();");
            gvr.Cells[3].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[3].Attributes.Add("onmouseout", "hideToolTip();");



        }
    }
    protected void gvVenues_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;
        PlacementManager.AddressComponent oAddressC = new PlacementManager.AddressComponent();

        if (e.Row.RowType == DataControlRowType.DataRow && this.cbShowAddressToolTip.Checked)
        {
            int addressId = oAddressC.SelectPrimaryAddressForVenue(Int32.Parse(DataBinder.Eval(e.Row.DataItem, "VenueId").ToString())).AddressId;
            string toolTip = this.GetVenueName(DataBinder.Eval(e.Row.DataItem, "VenueId").ToString());
            toolTip += "<br /> Address:<br />" + oAddressC.GetFormattedAddress(addressId);
            toolTip = toolTip.Replace("&", "\\&");
            toolTip = toolTip.Replace("'", "\\'");
            toolTip = toolTip.Replace("(", "\\(");
            toolTip = toolTip.Replace(")", "\\)");

            gvr.Cells[0].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[0].Attributes.Add("onmouseout", "hideToolTip();");

            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");
            gvr.Cells[2].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[2].Attributes.Add("onmouseout", "hideToolTip();");

        }
    }
    protected void gvVenues_DataBound(object sender, EventArgs e)
    {
        //If there are no venues allocated then we want to dsiable the add eductors button
        //and add students button
        if (this.gvVenues.Rows.Count > 0)
        {
            

        }
        else
        {
            

        }
    }
    protected void gvVenues_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvVenues_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PlacementManager.LinkVenuePlacementTypeComponent oLvptC = new PlacementManager.LinkVenuePlacementTypeComponent();
        PlacementManager.LinkVenuePlacementType oLvpt = oLvptC.SelectOne(Int32.Parse(this.gvVenues.DataKeys[e.RowIndex].Value.ToString()));

        e.NewValues.Add("VenueId", oLvpt.VenueId);
        e.NewValues.Add("PlacementTypeId", oLvpt.PlacementTypeId);
        e.NewValues.Add("IsActive", oLvpt.IsActive);
        e.NewValues.Add("CreatedBy", oLvpt.CreatedBy);
        e.NewValues.Add("DateCreated", oLvpt.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);
    }
    protected void gvVenues_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        e.KeepInEditMode = false;
    }
    protected void gvVenues_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void gvVenues_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ddShowVenues_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (this.ddShowVenues.SelectedValue)
        {
            case "0":
                this.gvAvailableVenues.DataSourceID = "odsAvailableVenuesAssocWUnitWCampus";
                this.DataBind();
                break;
            case "1":
                this.gvAvailableVenues.DataSourceID = "odsAvailableVenuesAssocWUnit";
                this.DataBind();
                break;
            case "2":
                this.gvAvailableVenues.DataSourceID = "odsAvailableVenuesAssocWCategory";
                this.DataBind();
                break;
            case "3":
                this.gvAvailableVenues.DataSourceID = "odsAvailableVenues";
                this.DataBind();
                break;
            default:
                break;
        }

    }
    protected void btnAddCheckedVenues_Click(object sender, EventArgs e)
    {
        PlacementManager.LinkVenuePlacementTypeComponent oLvptC = new PlacementManager.LinkVenuePlacementTypeComponent();
        int index = 0;
        int venueId = 0;

        //Loop through the grid and see if the checkbox is ticked
        //If it is then insert an association between the venue and
        //the placement type.
        foreach (GridViewRow gvr in this.gvAvailableVenues.Rows)
        {
            if (gvr.RowType == DataControlRowType.DataRow)
            {
                CheckBox RowLevelCheckBox = (CheckBox)gvr.FindControl("RowLevelCheckBox");
                if (RowLevelCheckBox.Checked)
                {
                    venueId = Int32.Parse(this.gvAvailableVenues.DataKeys[index].Value.ToString());

                    PlacementManager.LinkVenuePlacementType oLvpt = new PlacementManager.LinkVenuePlacementType();
                    oLvpt.CreatedBy = User.Identity.Name;
                    oLvpt.DateCreated = DateTime.Now;
                    oLvpt.DateUpdated = DateTime.Now;
                    oLvpt.IsActive = true;
                    oLvpt.IsRestricted = false;
                    oLvpt.MaxNumStudents = 0;
                    oLvpt.PlacementTypeId = Int32.Parse(this.gvPlacementTypes.SelectedDataKey.Value.ToString());
                    oLvpt.UpdatedBy = User.Identity.Name;
                    oLvpt.VenueId = venueId;
                    oLvptC.Insert(oLvpt);
                }
            }
            index++;
        }

        //Rebind the grids
        this.gvAvailableVenues.DataBind();
        this.gvVenues.DataBind();
    }
    protected void gvAvailableVenues_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;
        PlacementManager.AddressComponent oAddressC = new PlacementManager.AddressComponent();

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int addressId = oAddressC.SelectPrimaryAddressForVenue(Int32.Parse(DataBinder.Eval(e.Row.DataItem, "VenueId").ToString())).AddressId;
            string toolTip = DataBinder.Eval(e.Row.DataItem, "VenueName").ToString();
            toolTip += "<br /> Address:<br />" + oAddressC.GetFormattedAddress(addressId);
            toolTip = toolTip.Replace("&","\\&");
            toolTip = toolTip.Replace("'","\\'");
            toolTip = toolTip.Replace("(", "\\(");
            toolTip = toolTip.Replace(")","\\)");

            gvr.Cells[0].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[0].Attributes.Add("onmouseout", "hideToolTip();");

            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");

        }
    }
    protected void btnRemoveCheckedVenues_Click(object sender, EventArgs e)
    {
        
    }
    protected bool CheckIfStudentsAllocated(string sVenuePlacementTypeId)
    {
        PlacementManager.AllocationComponent oAllocationC = new PlacementManager.AllocationComponent();
        List<PlacementManager.Allocation> oAllocationList = oAllocationC.SelectAllWVenuePlacementTypeId(Int32.Parse(sVenuePlacementTypeId), "VenuePlacementTypeId");
        if (oAllocationList.Count > 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    protected void ddUnitFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
        
    }
    protected void ddCampusFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
        
    }
    protected void ddSemesterFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
        
    }
    protected void ddYearFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
       
    }

    protected void TestPB()
    {
        //this.Label2.Text = DateTime.Now.ToLongTimeString();
        //this.Label3.Text = DateTime.Now.ToLongTimeString();
    }
    protected void cbShowAddressToolTip_CheckedChanged(object sender, EventArgs e)
    {
        this.gvVenues.DataBind();
    }
    protected void btnCheckedVenuesAction_Click(object sender, EventArgs e)
    {
        switch (this.ddCheckedVenueAction.SelectedValue)
        {
            case "Remove":
                this.RemoveCheckedVenues();
                break;
            case "Unconfirm":
                this.ConfirmOrUnconfirmCheckedVenues("Unconfirm");
                break;
            case "Confirm":
                this.ConfirmOrUnconfirmCheckedVenues("Confirm");
                break;
            default:
                break;
        }
    }

    protected string GetConfirmedStatus(string isConfirmed)
    {
        string s = "<span style=\"color:#C00;\">Unconfirmed</span>";
        if (isConfirmed == "True")
        {
            s = "<span style=\"color:#090;\">Confirmed</span>";
        }

        return s;
    }

    protected void RemoveCheckedVenues()
    {
        PlacementManager.LinkVenuePlacementTypeComponent oLvptC = new PlacementManager.LinkVenuePlacementTypeComponent();
        int index = 0;
        int venuePlacementTypeId = 0;

        //Loop through the grid and see if the checkbox is ticked
        //If it is then delete the association between the venue and
        //the placement type.
        foreach (GridViewRow gvrVenues in this.gvVenues.Rows)
        {
            if (gvrVenues.RowType == DataControlRowType.DataRow)
            {
                CheckBox RowLevelCheckBox = (CheckBox)gvrVenues.FindControl("RowLevelCheckBox");
                if (RowLevelCheckBox.Checked)
                {
                    venuePlacementTypeId = Int32.Parse(this.gvVenues.DataKeys[index].Value.ToString());
                    oLvptC.Delete(venuePlacementTypeId);
                }
            }
            index++;
        }

        //Rebind the grids
        this.gvAvailableVenues.DataBind();
        this.gvVenues.DataBind();
    }

    protected void ConfirmOrUnconfirmCheckedVenues(string confirmOrUnconfirm)
    {
        PlacementManager.LinkVenuePlacementTypeComponent oLvptC = new PlacementManager.LinkVenuePlacementTypeComponent();
        int index = 0;
        int venuePlacementTypeId = 0;

        //Loop through the grid and see if the checkbox is ticked
        //If it is then delete the association between the venue and
        //the placement type.
        foreach (GridViewRow gvrVenues in this.gvVenues.Rows)
        {
            if (gvrVenues.RowType == DataControlRowType.DataRow)
            {
                CheckBox RowLevelCheckBox = (CheckBox)gvrVenues.FindControl("RowLevelCheckBox");
                if (RowLevelCheckBox.Checked)
                {
                    venuePlacementTypeId = Int32.Parse(this.gvVenues.DataKeys[index].Value.ToString());
                    PlacementManager.LinkVenuePlacementType oLvpt = oLvptC.SelectOne(venuePlacementTypeId);
                    if (confirmOrUnconfirm == "Confirm")
                    {
                        oLvpt.IsConfirmedByVenue = true;
                    }
                    else
                    {
                        oLvpt.IsConfirmedByVenue = false;
                    }

                    oLvptC.Update(oLvpt);
                }
            }
            index++;
        }

        //Rebind the grids
        this.gvAvailableVenues.DataBind();
        this.gvVenues.DataBind();
    }
}
